package net.expectx.pay.merchant.tag;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 富文本编辑器
 * @author lijian
 */
public class UEditorTag extends TagSupport {
    private static final String DEFAULT_MESSAGE_TIP="字数不要超过10000";
    private static final String DEFAULT_TOOL_BAR="FullScreen,Source,Undo, Redo,Bold,test,forecolor,fontsize,simpleupload,insertorderedlist,insertunorderedlist,imagecenter";
    private static final String DEFAULT_FILE_CATALOG="image";
    /**
     * 必填
     * 组件ID
     */
    private String uEditorId;
    /**
     * 回显内容
     * 选填
     */
    private String uEditorContent;
    /**
     * 最大字符数
     * 选填 默认10000
     */
    private Integer maximumWords;
    /**
     * 初始高度
     * 选填 默认350
     */
    private Integer initialFrameHeight;
    /**
     * 编辑器在页面中是第几个
     * 选填 默认1
     */
    private Integer uEditorNum;
    /**
     * 自定义工具栏
     * 选填
     */
    private String toolbar;
    /**
     * 信息提示
     * 选填
     */
    private String messageTip;

    /**
     * 是否上传至本地
     * 选填
     * 默认 true
     */
    private Boolean isLocal;

    /**
     * 文件上传云服务
     * 选填
     * 默认alioss
     */
    private String oss;
    /**
     * 文件大小限制
     * 选填
     * 默认2M
     */
    private Integer sizeLimit;

    @Override
    public int doEndTag() throws JspException {
        HttpServletRequest request=(HttpServletRequest) this.pageContext.getRequest();
        StringBuffer buffer=new StringBuffer();
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

        String server=basePath+"manager/file/upload.action?sizeLimit="+((null==sizeLimit)?1:sizeLimit);
        isLocal=null==isLocal?true:isLocal;
        if (!isLocal){
            server+="&oss="+(null==oss?"alioss":oss);
        }

        buffer.append("<script id=\""+uEditorId+"\" name=\""+uEditorId+"\" type=\"text/plain\">"+(null==uEditorContent?"":uEditorContent)+"</script>\n");
        if(null!=messageTip){
            buffer.append("<div class=\"am-article-meta\" style='margin-top: 10px'>要求:"+messageTip+".</div>\n");
        }else{
            buffer.append("<div class=\"am-article-meta\" style='margin-top: 10px'>要求:"+DEFAULT_MESSAGE_TIP+".</div>\n");
        }
        Map<String,Object> ueditorOptions=new HashMap<String,Object>(2);
        ueditorOptions.put("initialFrameHeight",(null==initialFrameHeight || initialFrameHeight<100)?350:initialFrameHeight);
        List<String> toolBarList=new ArrayList<String>();
        if (null!=toolbar){
            for (String s:toolbar.split(",") ) {
                toolBarList.add(s);
            }
        }else{
            for (String s:DEFAULT_TOOL_BAR.split(",") ) {
                toolBarList.add(s);
            }
        }
        List <List<String>>toolBarListTotal=new ArrayList<List <String>>();
        toolBarListTotal.add(toolBarList);
        ueditorOptions.put("toolbars",toolBarListTotal);
        ueditorOptions.put("maximumWords",(null==maximumWords)?10000:maximumWords);
        ueditorOptions.put("initialFrameWidth","100%");

        buffer.append("<script>\n");
        buffer.append("var "+uEditorId+"UE = UE.getEditor('"+uEditorId+"',"+ JSONObject.toJSONString(ueditorOptions)+");\n");
        if (null==uEditorNum ||uEditorNum==1){
            buffer.append("UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;\n");
            buffer.append("UE.Editor.prototype.getActionUrl = function(action) {\n");

            buffer.append("console.log(action);\n");
            buffer.append("if (action == 'uploadimage') {\n");
            buffer.append("return '"+server+"&fileType=images&catalog=image';\n");
            buffer.append(" }else if(action == 'uploadfile'){\n");
            buffer.append("return '"+server+"&fileType=file&catalog=file';\n");
            buffer.append(" }else if(action == 'uploadscrawl'){\n");
            buffer.append("return '"+basePath+"manager/file/uploadBase64.action?sizeLimit="+((null==sizeLimit)?1:sizeLimit)+"&fileType=images&catalog=image';\n");
            buffer.append(" }else if(action == 'uploadvideo'){\n");
            buffer.append("return '"+server+"&fileType=video&catalog=video';\n");
            buffer.append(" } else {\n");
            buffer.append(" return this._bkGetActionUrl.call(this, action);\n");
            buffer.append(" }\n};\n");
            buffer.append("UE.Editor.prototype._bkGetContentLength = UE.Editor.prototype.getContentLength;\n");
            buffer.append(" UE.Editor.prototype.getContentLength = function(){\n" +
                    "        return this.getContent().length;\n" +
                    "    };\n");
        }
        buffer.append("</script>");
        JspWriter out=this.pageContext.getOut();
        try {
            out.println(buffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.doEndTag();
    }
    public String errorTag(String msg){
        return "<div style='color:red'>"+msg+"</div>";
    }

    public String getuEditorId() {
        return uEditorId;
    }

    public void setuEditorId(String uEditorId) {
        this.uEditorId = uEditorId;
    }

    public String getuEditorContent() {
        return uEditorContent;
    }

    public void setuEditorContent(String uEditorContent) {
        this.uEditorContent = uEditorContent;
    }

    public Integer getInitialFrameHeight() {
        return initialFrameHeight;
    }

    public void setInitialFrameHeight(Integer initialFrameHeight) {
        this.initialFrameHeight = initialFrameHeight;
    }



    public Boolean getIsLocal() {
        return isLocal;
    }

    public void setIsLocal(Boolean local) {
        isLocal = local;
    }

    public String getOss() {
        return oss;
    }

    public void setOss(String oss) {
        this.oss = oss;
    }

    public Integer getSizeLimit() {
        return sizeLimit;
    }

    public void setSizeLimit(Integer sizeLimit) {
        this.sizeLimit = sizeLimit;
    }

    public Integer getuEditorNum() {
        return uEditorNum;
    }

    public void setuEditorNum(Integer uEditorNum) {
        this.uEditorNum = uEditorNum;
    }

    public String getMessageTip() {
        return messageTip;
    }

    public void setMessageTip(String messageTip) {
        this.messageTip = messageTip;
    }

    public String getToolbar() {
        return toolbar;
    }

    public void setToolbar(String toolbar) {
        this.toolbar = toolbar;
    }

    public Integer getMaximumWords() {
        return maximumWords;
    }

    public void setMaximumWords(Integer maximumWords) {
        this.maximumWords = maximumWords;
    }
}
